home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C028.ZIP / SZ.C < prev    next >
Text File  |  1990-01-31  |  38KB  |  1,710 lines

  1. #define VERSION "sz 2.12 05-29-88"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6.     Following is used for testing, might not be reasonable for production
  7. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  8.  
  9.  ****************************************************************************
  10.  *
  11.  * sz.c By Chuck Forsberg,  Omen Technology INC
  12.  *
  13.  ****************************************************************************
  14.  *
  15.  * Typical Unix/Xenix/Clone compiles:
  16.  *
  17.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  18.  *    cc -O -DSV sz.c -o sz        Sys V Release 2 with non-blocking input
  19.  *                    Define to allow reverse channel checking
  20.  *    cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  21.  *
  22.  *    cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz    Classic Xenix
  23.  *
  24.  *    ln sz sb            **** All versions ****
  25.  *    ln sz sx            **** All versions ****
  26.  *
  27.  ****************************************************************************
  28.  *
  29.  * Typical VMS compile and install sequence:
  30.  *
  31.  *        define LNK$LIBRARY   SYS$LIBRARY:VAXCRTL.OLB
  32.  *        cc sz.c
  33.  *        cc vvmodem.c
  34.  *        link sz,vvmodem
  35.  *    sz :== $disk$user2:[username.subdir]sz.exe
  36.  *
  37.  *  If you feel adventureous, remove the #define BADSYNC line
  38.  *  immediately following the #ifdef vax11c line!  Some VMS
  39.  *  systems know how to fseek, some don't.
  40.  *
  41.  ****************************************************************************
  42.  *
  43.  *
  44.  * A program for Unix to send files and commands to computers running
  45.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  46.  *
  47.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  48.  *
  49.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  50.  *
  51.  *  2.1x hacks to avoid VMS fseek() bogosity, allow input from pipe
  52.  *     -DBADSEEK -DTXBSIZE=32768  
  53.  *  2.x has mods for VMS flavor
  54.  *
  55.  * 1.34 implements tx backchannel garbage count and ZCRCW after ZRPOS
  56.  * in accordance with the 7-31-87 ZMODEM Protocol Description
  57.  */
  58.  
  59.  
  60. char *substr(), *getenv();
  61.  
  62. #ifdef vax11c
  63. #define BADSEEK
  64. #define TXBSIZE 32768        /* Must be power of two, < MAXINT */
  65. #include <types.h>
  66. #include <stat.h>
  67. #define LOGFILE "szlog.tmp"
  68. #include <stdio.h>
  69. #include <signal.h>
  70. #include <setjmp.h>
  71. #include <ctype.h>
  72. #include <errno.h>
  73. #define OS "VMS"
  74. #define READCHECK
  75. #define BUFWRITE
  76. #define iofd
  77. extern int errno;
  78. #define SS_NORMAL SS$_NORMAL
  79. #define xsendline(c) sendline(c)
  80.  
  81.  
  82. #else    /* vax11c */
  83.  
  84.  
  85. #define SS_NORMAL 0
  86. #define LOGFILE "/tmp/szlog"
  87. #include <stdio.h>
  88. #include <signal.h>
  89. #include <setjmp.h>
  90. #include <ctype.h>
  91. #include <errno.h>
  92. extern int errno;
  93.  
  94. #define sendline(c) putchar(c & 0377)
  95. #define xsendline(c) putchar(c)
  96.  
  97. #endif
  98.  
  99. #define PATHLEN 256
  100. #define OK 0
  101. #define FALSE 0
  102. #define TRUE 1
  103. #define ERROR (-1)
  104. /* Ward Christensen / CP/M parameters - Don't change these! */
  105. #define ENQ 005
  106. #define CAN ('X'&037)
  107. #define XOFF ('s'&037)
  108. #define XON ('q'&037)
  109. #define SOH 1
  110. #define STX 2
  111. #define EOT 4
  112. #define ACK 6
  113. #define NAK 025
  114. #define CPMEOF 032
  115. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  116. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  117. #define TIMEOUT (-2)
  118. #define RCDO (-3)
  119. #define RETRYMAX 10
  120.  
  121.  
  122. #define HOWMANY 2
  123. int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  124. unsigned Baudrate=2400;    /* Default, should be set by first mode() call */
  125. unsigned Txwindow;    /* Control the size of the transmitted window */
  126. unsigned Txwspac;    /* Spacing between zcrcq requests */
  127. unsigned Txwcnt;    /* Counter used to space ack requests */
  128. long Lrxpos;        /* Receiver's last reported offset */
  129. int errors;
  130.  
  131. #ifdef vax11c
  132. #include "vrzsz.c"    /* most of the system dependent stuff here */
  133. #else
  134. #include "rbsb.c"    /* most of the system dependent stuff here */
  135. #endif
  136. #include "crctab.c"
  137.  
  138. int Filesleft;
  139. long Totalleft;
  140.  
  141. /*
  142.  * Attention string to be executed by receiver to interrupt streaming data
  143.  *  when an error is detected.  A pause (0336) may be needed before the
  144.  *  ^C (03) or after it.
  145.  */
  146. #ifdef READCHECK
  147. char Myattn[] = { 0 };
  148. #else
  149. #ifdef USG
  150. char Myattn[] = { 03, 0336, 0 };
  151. #else
  152. char Myattn[] = { 0 };
  153. #endif
  154. #endif
  155.  
  156. FILE *in;
  157.  
  158. #ifdef BADSEEK
  159. int Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  160. #ifndef TXBSIZE
  161. #define TXBSIZE 16384        /* Must be power of two, < MAXINT */
  162. #endif
  163. #else
  164. int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  165. #endif
  166.  
  167. #ifdef TXBSIZE
  168. #define TXBMASK (TXBSIZE-1)
  169. char Txb[TXBSIZE];        /* Circular buffer for file reads */
  170. char *txbuf = Txb;        /* Pointer to current file segment */
  171. #else
  172. char txbuf[1024];
  173. #endif
  174. long vpos = 0;            /* Number of bytes read from file */
  175.  
  176. char Lastrx;
  177. char Crcflg;
  178. int Verbose=0;
  179. int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  180. int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  181. int Quiet=0;        /* overrides logic that would otherwise set verbose */
  182. int Ascii=0;        /* Add CR's for brain damaged programs */
  183. int Fullname=0;        /* transmit full pathname */
  184. int Unlinkafter=0;    /* Unlink file after it is sent */
  185. int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  186. int firstsec;
  187. int errcnt=0;        /* number of files unreadable */
  188. int blklen=128;        /* length of transmitted records */
  189. int Optiong;        /* Let it rip no wait for sector ACK's */
  190. int Eofseen;        /* EOF seen on input set by zfilbuf */
  191. int BEofseen;        /* EOF seen on input set by fooseek */
  192. int Totsecs;        /* total number of sectors this file */
  193. int Filcnt=0;        /* count of number of files opened */
  194. int Lfseen=0;
  195. unsigned Rxbuflen = 16384;    /* Receiver's max buffer length */
  196. int Tframlen = 0;    /* Override for tx frame length */
  197. int blkopt=0;        /* Override value for zmodem blklen */
  198. int Rxflags = 0;
  199. long bytcnt;
  200. int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  201. char Lzconv;    /* Local ZMODEM file conversion request */
  202. char Lzmanag;    /* Local ZMODEM file management request */
  203. int Lskipnocor;
  204. char Lztrans;
  205. char zconv;        /* ZMODEM file conversion request */
  206. char zmanag;        /* ZMODEM file management request */
  207. char ztrans;        /* ZMODEM file transport request */
  208. int Command;        /* Send a command, then exit. */
  209. char *Cmdstr;        /* Pointer to the command string */
  210. int Cmdtries = 11;
  211. int Cmdack1;        /* Rx ACKs command, then do it */
  212. int Exitcode;
  213. int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  214.             /* 2= Character transparency test */
  215. char *qbf="The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  216. long Lastsync;        /* Last offset to which we got a ZRPOS */
  217. int Beenhereb4;        /* How many times we've been ZRPOS'd same place */
  218.  
  219. jmp_buf tohere;        /* For the interrupt on RX timeout */
  220. jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  221.  
  222. /* called by signal interrupt or terminate to clean things up */
  223. bibi(n)
  224. {
  225.     canit(); fflush(stdout); mode(0);
  226.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  227.     if (n == SIGQUIT)
  228.         abort();
  229.     if (n == 99)
  230.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  231.     cucheck();
  232.     exit(128+n);
  233. }
  234. /* Called when ZMODEM gets an interrupt (^X) */
  235. onintr()
  236. {
  237.     signal(SIGINT, SIG_IGN);
  238.     longjmp(intrjmp, -1);
  239. }
  240.  
  241. int Zctlesc;    /* Encode control characters */
  242. int Nozmodem = 0;    /* If invoked as "sb" */
  243. char *Progname = "sz";
  244. int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  245. #include "zm.c"
  246.  
  247.  
  248. main(argc, argv)
  249. char *argv[];
  250. {
  251.     register char *cp;
  252.     register npats;
  253.     int dm;
  254.     char **patts;
  255.     static char xXbuf[BUFSIZ];
  256.  
  257.     if ((cp = getenv("ZNULLS")) && *cp)
  258.         Znulls = atoi(cp);
  259.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  260.         Restricted=TRUE;
  261.     from_cu();
  262.     chkinvok(argv[0]);
  263.  
  264.     Rxtimeout = 600;
  265.     npats=0;
  266.     if (argc<2)
  267.         usage();
  268.     setbuf(stdout, xXbuf);        
  269.     while (--argc) {
  270.         cp = *++argv;
  271.         if (*cp++ == '-' && *cp) {
  272.             while ( *cp) {
  273.                 switch(*cp++) {
  274.                 case '\\':
  275.                      *cp = toupper(*cp);  continue;
  276.                 case '+':
  277.                     Lzmanag = ZMAPND; break;
  278. #ifdef CSTOPB
  279.                 case '2':
  280.                     Twostop = TRUE; break;
  281. #endif
  282.                 case 'a':
  283.                     Lzconv = ZCNL;
  284.                     Ascii = TRUE; break;
  285.                 case 'b':
  286.                     Lzconv = ZCBIN; break;
  287.                 case 'C':
  288.                     if (--argc < 1) {
  289.                         usage();
  290.                     }
  291.                     Cmdtries = atoi(*++argv);
  292.                     break;
  293.                 case 'i':
  294.                     Cmdack1 = ZCACK1;
  295.                     /* **** FALL THROUGH TO **** */
  296.                 case 'c':
  297.                     if (--argc != 1) {
  298.                         usage();
  299.                     }
  300.                     Command = TRUE;
  301.                     Cmdstr = *++argv;
  302.                     break;
  303.                 case 'd':
  304.                     ++Dottoslash;
  305.                     /* **** FALL THROUGH TO **** */
  306.                 case 'f':
  307.                     Fullname=TRUE; break;
  308.                 case 'e':
  309.                     Zctlesc = 1; break;
  310.                 case 'k':
  311.                     blklen=1024; break;
  312.                 case 'L':
  313.                     if (--argc < 1) {
  314.                         usage();
  315.                     }
  316.                     blkopt = atoi(*++argv);
  317.                     if (blkopt<24 || blkopt>1024)
  318.                         usage();
  319.                     break;
  320.                 case 'l':
  321.                     if (--argc < 1) {
  322.                         usage();
  323.                     }
  324.                     Tframlen = atoi(*++argv);
  325.                     if (Tframlen<32 || Tframlen>1024)
  326.                         usage();
  327.                     break;
  328.                 case 'N':
  329.                     Lzmanag = ZMNEWL;  break;
  330.                 case 'n':
  331.                     Lzmanag = ZMNEW;  break;
  332.                 case 'o':
  333.                     Wantfcs32 = FALSE; break;
  334.                 case 'p':
  335.                     Lzmanag = ZMPROT;  break;
  336.                 case 'r':
  337.                     Lzconv = ZCRESUM;
  338.                 case 'q':
  339.                     Quiet=TRUE; Verbose=0; break;
  340.                 case 't':
  341.                     if (--argc < 1) {
  342.                         usage();
  343.                     }
  344.                     Rxtimeout = atoi(*++argv);
  345.                     if (Rxtimeout<10 || Rxtimeout>1000)
  346.                         usage();
  347.                     break;
  348.                 case 'T':
  349.                     if (++Test > 1) {
  350.                         chartest(1); chartest(2);
  351.                         mode(0);  exit(0);
  352.                     }
  353.                     break;
  354. #ifndef vax11c
  355.                 case 'u':
  356.                     ++Unlinkafter; break;
  357. #endif
  358.                 case 'v':
  359.                     ++Verbose; break;
  360.                 case 'w':
  361.                     if (--argc < 1) {
  362.                         usage();
  363.                     }
  364.                     Txwindow = atoi(*++argv);
  365.                     if (Txwindow < 256)
  366.                         Txwindow = 256;
  367.                     Txwindow = (Txwindow/64) * 64;
  368.                     Txwspac = Txwindow/4;
  369.                     if (blkopt > Txwspac
  370.                      || (!blkopt && Txwspac < 1024))
  371.                         blkopt = Txwspac;
  372.                     break;
  373.                 case 'X':
  374.                     ++Modem2; break;
  375.                 case 'Y':
  376.                     Lskipnocor = TRUE;
  377.                     /* **** FALLL THROUGH TO **** */
  378.                 case 'y':
  379.                     Lzmanag = ZMCLOB; break;
  380.                 default:
  381.                     usage();
  382.                 }
  383.             }
  384.         }
  385.         else if ( !npats && argc>0) {
  386.             if (argv[0][0]) {
  387.                 npats=argc;
  388.                 patts=argv;
  389. #ifndef vax11c
  390.                 if ( !strcmp(*patts, "-"))
  391.                     iofd = 1;
  392. #endif
  393.             }
  394.         }
  395.     }
  396.     if (npats < 1 && !Command && !Test) 
  397.         usage();
  398.     if (Verbose) {
  399.         if (freopen(LOGFILE, "a", stderr)==NULL) {
  400.             printf("Can't open log file %s\n",LOGFILE);
  401.             exit(0200);
  402.         }
  403.         setbuf(stderr, NULL);
  404.     }
  405.     if (Fromcu && !Quiet) {
  406.         if (Verbose == 0)
  407.             Verbose = 2;
  408.     }
  409.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  410.  
  411.     mode(1);
  412.  
  413.     if (signal(SIGINT, bibi) == SIG_IGN) {
  414.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  415.     } else {
  416.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  417.     }
  418.     if ( !Fromcu)
  419.         signal(SIGQUIT, SIG_IGN);
  420.     signal(SIGTERM, bibi);
  421.  
  422.     if ( !Modem2) {
  423.         if (!Nozmodem) {
  424.             printf("rz\r");  fflush(stdout);
  425.         }
  426.         countem(npats, patts);
  427.         if (!Nozmodem) {
  428.             stohdr(0L);
  429.             if (Command)
  430.                 Txhdr[ZF0] = ZCOMMAND;
  431.             zshhdr(ZRQINIT, Txhdr);
  432.         }
  433.     }
  434.     fflush(stdout);
  435.  
  436.     if (Command) {
  437.         if (getzrxinit()) {
  438.             Exitcode=0200; canit();
  439.         }
  440.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  441.             Exitcode=0200; canit();
  442.         }
  443.     } else if (wcsend(npats, patts)==ERROR) {
  444.         Exitcode=0200;
  445.         canit();
  446.     }
  447.     fflush(stdout);
  448.     mode(0);
  449.     dm = ((errcnt != 0) | Exitcode);
  450.     if (dm) {
  451.         cucheck();  exit(dm);
  452.     }
  453.     exit(SS_NORMAL);
  454.     /*NOTREACHED*/
  455. }
  456.  
  457. wcsend(argc, argp)
  458. char *argp[];
  459. {
  460.     register n;
  461.  
  462.     Crcflg=FALSE;
  463.     firstsec=TRUE;
  464.     bytcnt = -1;
  465.     for (n=0; n<argc; ++n) {
  466.         Totsecs = 0;
  467.         if (wcs(argp[n])==ERROR)
  468.             return ERROR;
  469.     }
  470.     Totsecs = 0;
  471.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  472.         if ( !Modem2) {
  473.             Command = TRUE;
  474.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  475.             if (getnak()) {
  476.                 Exitcode=0200; canit();
  477.             }
  478.             if (!Zmodem)
  479.                 canit();
  480.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  481.                 Exitcode=0200; canit();
  482.             }
  483.             Exitcode = 1; return OK;
  484.         }
  485.         canit();
  486.         fprintf(stderr,"\r\nCan't open any requested files.\r\n");
  487.         return ERROR;
  488.     }
  489.     if (Zmodem)
  490.         saybibi();
  491.     else if ( !Modem2)
  492.         wctxpn("");
  493.     return OK;
  494. }
  495.  
  496. wcs(oname)
  497. char *oname;
  498. {
  499.     register c;
  500.     register char *p;
  501.     struct stat f;
  502.     char name[PATHLEN];
  503.  
  504.     strcpy(name, oname);
  505.  
  506.     if (Restricted) {
  507.         /* restrict pathnames to current tree or uucppublic */
  508.         if ( substr(name, "../")
  509.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  510.             canit();
  511.             fprintf(stderr,"\r\nsz:\tSecurity Violation\r\n");
  512.             return ERROR;
  513.         }
  514.     }
  515.  
  516.     if ( !strcmp(oname, "-")) {
  517.         if ((p = getenv("ONAME")) && *p)
  518.             strcpy(name, p);
  519.         else
  520.             sprintf(name, "s%d.sz", getpid());
  521.         in = stdin;
  522.     }
  523.     else if ((in=fopen(oname, "r"))==NULL) {
  524.         ++errcnt;
  525.         return OK;    /* pass over it, there may be others */
  526.     }
  527.     BEofseen = Eofseen = 0;  vpos = 0;
  528.     /* Check for directory or block special files */
  529.     fstat(fileno(in), &f);
  530.     c = f.st_mode & S_IFMT;
  531.     if (c == S_IFDIR || c == S_IFBLK) {
  532.         fclose(in);
  533.         return OK;
  534.     }
  535.  
  536.     ++Filcnt;
  537.     switch (wctxpn(name)) {
  538.     case ERROR:
  539.         return ERROR;
  540.     case ZSKIP:
  541.         return OK;
  542.     }
  543.     if (!Zmodem && wctx(f.st_size)==ERROR)
  544.         return ERROR;
  545. #ifndef vax11c
  546.     if (Unlinkafter)
  547.         unlink(oname);
  548. #endif
  549.     return 0;
  550. }
  551.  
  552. /*
  553.  * generate and transmit pathname block consisting of
  554.  *  pathname (null terminated),
  555.  *  file length, mode time and file mode in octal
  556.  *  as provided by the Unix fstat call.
  557.  *  N.B.: modifies the passed name, may extend it!
  558.  */
  559. wctxpn(name)
  560. char *name;
  561. {
  562.     register char *p, *q;
  563.     char name2[PATHLEN];
  564.     struct stat f;
  565.  
  566.     if (Modem2) {
  567.         if ((in!=stdin) && *name && fstat(fileno(in), &f)!= -1) {
  568.             fprintf(stderr, "Sending %s, %ld blocks: ",
  569.               name, f.st_size>>7);
  570.         }
  571.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  572.         return OK;
  573.     }
  574.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  575.     if ( !Zmodem)
  576.         if (getnak())
  577.             return ERROR;
  578.  
  579.     q = (char *) 0;
  580.     if (Dottoslash) {        /* change . to . */
  581.         for (p=name; *p; ++p) {
  582.             if (*p == '/')
  583.                 q = p;
  584.             else if (*p == '.')
  585.                 *(q=p) = '/';
  586.         }
  587.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  588.             q += 8;            /*   make it .ext */
  589.             strcpy(name2, q);    /* save excess of name */
  590.             *q = '.';
  591.             strcpy(++q, name2);    /* add it back */
  592.         }
  593.     }
  594.  
  595.     for (p=name, q=txbuf ; *p; )
  596.         if ((*q++ = *p++) == '/' && !Fullname)
  597.             q = txbuf;
  598.     *q++ = 0;
  599.     p=q;
  600.     while (q < (txbuf + 1024))
  601.         *q++ = 0;
  602.     if (!Ascii && (in!=stdin) && *name && fstat(fileno(in), &f)!= -1)
  603.         sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  604.           f.st_mode, Filesleft, Totalleft);
  605.     Totalleft -= f.st_size;
  606.     if (--Filesleft <= 0)
  607.         Totalleft = 0;
  608.     if (Totalleft < 0)
  609.         Totalleft = 0;
  610.  
  611.     /* force 1k blocks if name won't fit in 128 byte block */
  612.     if (txbuf[125])
  613.         blklen=1024;
  614.     else {        /* A little goodie for IMP/KMD */
  615.         txbuf[127] = (f.st_size + 127) >>7;
  616.         txbuf[126] = (f.st_size + 127) >>15;
  617.     }
  618.     if (Zmodem)
  619.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  620.     if (wcputsec(txbuf, 0, 128)==ERROR)
  621.         return ERROR;
  622.     return OK;
  623. }
  624.  
  625. getnak()
  626. {
  627.     register firstch;
  628.  
  629.     Lastrx = 0;
  630.     for (;;) {
  631.         switch (firstch = readline(800)) {
  632.         case ZPAD:
  633.             if (getzrxinit())
  634.                 return ERROR;
  635.             Ascii = 0;    /* Receiver does the conversion */
  636.             return FALSE;
  637.         case TIMEOUT:
  638.             zperr("Timeout on pathname");
  639.             return TRUE;
  640.         case WANTG:
  641. #ifdef MODE2OK
  642.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  643. #endif
  644.             Optiong = TRUE;
  645.             blklen=1024;
  646.         case WANTCRC:
  647.             Crcflg = TRUE;
  648.         case NAK:
  649.             return FALSE;
  650.         case CAN:
  651.             if ((firstch = readline(20)) == CAN && Lastrx == CAN)
  652.                 return TRUE;
  653.         default:
  654.             break;
  655.         }
  656.         Lastrx = firstch;
  657.     }
  658. }
  659.  
  660.  
  661. wctx(flen)
  662. long flen;
  663. {
  664.     register int thisblklen;
  665.     register int sectnum, attempts, firstch;
  666.     long charssent;
  667.  
  668.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  669.     vfile("wctx:file length=%ld", flen);
  670.  
  671.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  672.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  673.         ;
  674.     if (firstch==CAN) {
  675.         zperr("Receiver CANcelled");
  676.         return ERROR;
  677.     }
  678.     if (firstch==WANTCRC)
  679.         Crcflg=TRUE;
  680.     if (firstch==WANTG)
  681.         Crcflg=TRUE;
  682.     sectnum=0;
  683.     for (;;) {
  684.         if (flen <= (charssent + 896L))
  685.             thisblklen = 128;
  686.         if ( !filbuf(txbuf, thisblklen))
  687.             break;
  688.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  689.             return ERROR;
  690.         charssent += thisblklen;
  691.     }
  692.     fclose(in);
  693.     attempts=0;
  694.     do {
  695.         purgeline();
  696.         sendline(EOT);
  697.         fflush(stdout);
  698.         ++attempts;
  699.     }
  700.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  701.     if (attempts == RETRYMAX) {
  702.         zperr("No ACK on EOT");
  703.         return ERROR;
  704.     }
  705.     else
  706.         return OK;
  707. }
  708.  
  709. wcputsec(buf, sectnum, cseclen)
  710. char *buf;
  711. int sectnum;
  712. int cseclen;    /* data length of this sector to send */
  713. {
  714.     register checksum, wcj;
  715.     register char *cp;
  716.     unsigned oldcrc;
  717.     int firstch;
  718.     int attempts;
  719.  
  720.     firstch=0;    /* part of logic to detect CAN CAN */
  721.  
  722.     if (Verbose>2)
  723.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  724.     else if (Verbose>1)
  725.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  726.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  727.         Lastrx= firstch;
  728.         sendline(cseclen==1024?STX:SOH);
  729.         sendline(sectnum);
  730.         sendline(-sectnum -1);
  731.         oldcrc=checksum=0;
  732.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  733.             sendline(*cp);
  734.             oldcrc=updcrc((0377& *cp), oldcrc);
  735.             checksum += *cp++;
  736.         }
  737.         if (Crcflg) {
  738.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  739.             sendline((int)oldcrc>>8);
  740.             sendline((int)oldcrc);
  741.         }
  742.         else
  743.             sendline(checksum);
  744.  
  745.         if (Optiong) {
  746.             firstsec = FALSE; return OK;
  747.         }
  748.         firstch = readline(Rxtimeout);
  749. gotnak:
  750.         switch (firstch) {
  751.         case CAN:
  752.             if(Lastrx == CAN) {
  753. cancan:
  754.                 zperr("Cancelled");  return ERROR;
  755.             }
  756.             break;
  757.         case TIMEOUT:
  758.             zperr("Timeout on sector ACK"); continue;
  759.         case WANTCRC:
  760.             if (firstsec)
  761.                 Crcflg = TRUE;
  762.         case NAK:
  763.             zperr("NAK on sector"); continue;
  764.         case ACK: 
  765.             firstsec=FALSE;
  766.             Totsecs += (cseclen>>7);
  767.             return OK;
  768.         case ERROR:
  769.             zperr("Got burst for sector ACK"); break;
  770.         default:
  771.             zperr("Got %02x for sector ACK", firstch); break;
  772.         }
  773.         for (;;) {
  774.             Lastrx = firstch;
  775.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  776.                 break;
  777.             if (firstch == NAK || firstch == WANTCRC)
  778.                 goto gotnak;
  779.             if (firstch == CAN && Lastrx == CAN)
  780.                 goto cancan;
  781.         }
  782.     }
  783.     zperr("Retry Count Exceeded");
  784.     return ERROR;
  785. }
  786.  
  787. /* fill buf with count chars padding with ^Z for CPM */
  788. filbuf(buf, count)
  789. register char *buf;
  790. {
  791.     register c, m;
  792.  
  793.     if ( !Ascii) {
  794.         m = read(fileno(in), buf, count);
  795.         if (m <= 0)
  796.             return 0;
  797.         while (m < count)
  798.             buf[m++] = 032;
  799.         return count;
  800.     }
  801.     m=count;
  802.     if (Lfseen) {
  803.         *buf++ = 012; --m; Lfseen = 0;
  804.     }
  805.     while ((c=getc(in))!=EOF) {
  806.         if (c == 012) {
  807.             *buf++ = 015;
  808.             if (--m == 0) {
  809.                 Lfseen = TRUE; break;
  810.             }
  811.         }
  812.         *buf++ =c;
  813.         if (--m == 0)
  814.             break;
  815.     }
  816.     if (m==count)
  817.         return 0;
  818.     else
  819.         while (--m>=0)
  820.             *buf++ = CPMEOF;
  821.     return count;
  822. }
  823.  
  824. /* Fill buffer with blklen chars */
  825. zfilbuf()
  826. {
  827.     int n;
  828.  
  829. #ifdef TXBSIZE
  830.     /* We assume request is within buffer, or just beyond */
  831.     txbuf = Txb + (bytcnt & TXBMASK);
  832.     if (vpos <= bytcnt) {
  833.         n = fread(txbuf, 1, blklen, in);
  834.         vpos += n;
  835.         if (n < blklen)
  836.             Eofseen = 1;
  837.         return n;
  838.     }
  839.     if (vpos >= (bytcnt+blklen))
  840.         return blklen;
  841.     /* May be a short block if crash recovery etc. */
  842.     Eofseen = BEofseen;
  843.     return (vpos - bytcnt);
  844. #else
  845.     n = fread(txbuf, 1, blklen, in);
  846.     if (n < blklen)
  847.         Eofseen = 1;
  848.     return n;
  849. #endif
  850. }
  851.  
  852. #ifdef TXBSIZE
  853. fooseek(fptr, pos, whence)
  854. FILE *fptr;
  855. long pos;
  856. {
  857.     int m, n;
  858.  
  859.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  860.     /* Seek offset < current buffer */
  861.     if (pos < (vpos -TXBSIZE +1024)) {
  862.         BEofseen = 0;
  863.         if (Canseek > 0) {
  864.             vpos = pos & ~TXBMASK;
  865.             if (vpos >= pos)
  866.                 vpos -= TXBSIZE;
  867.             if (fseek(fptr, vpos, 0))
  868.                 return 1;
  869.         }
  870.         else if (Canseek == 0)
  871.             if (fseek(fptr, vpos = 0L, 0))
  872.                 return 1;
  873.         else
  874.             return 1;
  875.         while (vpos <= pos) {
  876.             n = fread(Txb, 1, TXBSIZE, fptr);
  877.             vpos += n;
  878.             vfile("n=%d vpos=%ld", n, vpos);
  879.             if (n < TXBSIZE) {
  880.                 BEofseen = 1;
  881.                 break;
  882.             }
  883.         }
  884.         vfile("vpos=%ld", vpos);
  885.         return 0;
  886.     }
  887.     /* Seek offset > current buffer (crash recovery, etc.) */
  888.     if (pos > vpos) {
  889.         if (Canseek)
  890.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  891.                 return 1;
  892.         while (vpos <= pos) {
  893.             txbuf = Txb + (vpos & TXBMASK);
  894.             m = TXBSIZE - (vpos & TXBMASK);
  895.             n = fread(txbuf, 1, m, fptr);
  896.             vpos += n;
  897.             vfile("bo=%d n=%d vpos=%ld", txbuf-Txb, n, vpos);
  898.             if (m < n) {
  899.                 BEofseen = 1;
  900.                 break;
  901.             }
  902.         }
  903.         return 0;
  904.     }
  905.     /* Seek offset is within current buffer */
  906.     vfile("vpos=%ld", vpos);
  907.     return 0;
  908. }
  909. #define fseek fooseek
  910. #endif
  911.  
  912.  
  913. /* VARARGS1 */
  914. vfile(f, a, b, c)
  915. register char *f;
  916. {
  917.     if (Verbose > 2) {
  918.         fprintf(stderr, f, a, b, c);
  919.         fprintf(stderr, "\n");
  920.     }
  921. }
  922.  
  923.  
  924. alrm()
  925. {
  926.     longjmp(tohere, -1);
  927. }
  928.  
  929.  
  930. #ifndef vax11c
  931. /*
  932.  * readline(timeout) reads character(s) from file descriptor 0
  933.  * timeout is in tenths of seconds
  934.  */
  935. readline(timeout)
  936. {
  937.     register int c;
  938.     static char byt[1];
  939.  
  940.     fflush(stdout);
  941.     if (setjmp(tohere)) {
  942.         zperr("TIMEOUT");
  943.         return TIMEOUT;
  944.     }
  945.     c = timeout/10;
  946.     if (c<2)
  947.         c=2;
  948.     if (Verbose>5) {
  949.         fprintf(stderr, "Timeout=%d Calling alarm(%d) ", timeout, c);
  950.     }
  951.     signal(SIGALRM, alrm); alarm(c);
  952.     c=read(iofd, byt, 1);
  953.     alarm(0);
  954.     if (Verbose>5)
  955.         fprintf(stderr, "ret %x\n", byt[0]);
  956.     if (c<1)
  957.         return TIMEOUT;
  958.     return (byt[0]&0377);
  959. }
  960.  
  961. flushmo()
  962. {
  963.     fflush(stdout);
  964. }
  965.  
  966.  
  967. purgeline()
  968. {
  969. #ifdef USG
  970.     ioctl(iofd, TCFLSH, 0);
  971. #else
  972.     lseek(iofd, 0L, 2);
  973. #endif
  974. }
  975. #endif
  976.  
  977. /* send cancel string to get the other end to shut up */
  978. canit()
  979. {
  980.     static char canistr[] = {
  981.      24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
  982.     };
  983.  
  984. #ifdef vax11c
  985.     raw_wbuf(strlen(canistr), canistr);
  986.     purgeline();
  987. #else
  988.     printf(canistr);
  989.     fflush(stdout);
  990. #endif
  991. }
  992.  
  993.  
  994. /*
  995.  * Log an error
  996.  */
  997. /*VARARGS1*/
  998. zperr(s,p,u)
  999. char *s, *p, *u;
  1000. {
  1001.     if (Verbose <= 0)
  1002.         return;
  1003.     fprintf(stderr, "Retry %d: ", errors);
  1004.     fprintf(stderr, s, p, u);
  1005.     fprintf(stderr, "\n");
  1006. }
  1007.  
  1008. /*
  1009.  * substr(string, token) searches for token in string s
  1010.  * returns pointer to token within string if found, NULL otherwise
  1011.  */
  1012. char *
  1013. substr(s, t)
  1014. register char *s,*t;
  1015. {
  1016.     register char *ss,*tt;
  1017.     /* search for first char of token */
  1018.     for (ss=s; *s; s++)
  1019.         if (*s == *t)
  1020.             /* compare token with substring */
  1021.             for (ss=s,tt=t; ;) {
  1022.                 if (*tt == 0)
  1023.                     return s;
  1024.                 if (*ss++ != *tt++)
  1025.                     break;
  1026.             }
  1027.     return NULL;
  1028. }
  1029.  
  1030. char *babble[] = {
  1031. #ifdef vax11c
  1032.     "    Send file(s) with ZMODEM Protocol",
  1033.     "Usage:    sz [-2+abdefkLlNnquvwYy] [-] file ...",
  1034.     "    sz [-2Ceqv] -c COMMAND",
  1035.     "    \\ Force next option letter to upper case",
  1036. #else
  1037.     "Send file(s) with ZMODEM/YMODEM/XMODEM Protocol",
  1038.     "    (Y) = Option applies to YMODEM only",
  1039.     "    (Z) = Option applies to ZMODEM only",
  1040.     "Usage:    sz [-2+abdefkLlNnquvwYy] [-] file ...",
  1041.     "    sz [-2Ceqv] -c COMMAND",
  1042.     "    sb [-2adfkquv] [-] file ...",
  1043.     "    sx [-2akquv] [-] file",
  1044. #endif
  1045. #ifdef CSTOPB
  1046.     "    2 Use 2 stop bits",
  1047. #endif
  1048.     "    + Append to existing destination file (Z)",
  1049.     "    a (ASCII) change NL to CR/LF",
  1050.     "    b Binary file transfer override",
  1051.     "    c send COMMAND (Z)",
  1052. #ifndef vax11c
  1053.     "    d Change '.' to '/' in pathnames (Y/Z)",
  1054. #endif
  1055.     "    e Escape all control characters (Z)",
  1056.     "    f send Full pathname (Y/Z)",
  1057.     "    i send COMMAND, ack Immediately (Z)",
  1058.     "    k Send 1024 byte packets (Y)",
  1059.     "    L N Limit subpacket length to N bytes (Z)",
  1060.     "    l N Limit frame length to N bytes (l>=L) (Z)",
  1061.     "    n send file if source newer (Z)",
  1062.     "    N send file if source newer or longer (Z)",
  1063.     "    o Use 16 bit CRC instead of 32 bit CRC (Z)",
  1064.     "    p Protect existing destination file (Z)",
  1065.     "    r Resume/Recover interrupted file transfer (Z)",
  1066.     "    q Quiet (no progress reports)",
  1067. #ifndef vax11c
  1068.     "    u Unlink file after transmission",
  1069. #endif
  1070.     "    v Verbose - provide debugging information",
  1071.     "    w N Window is N bytes (Z)",
  1072.     "    Y Yes, overwrite existing file, skip if not present at rx (Z)",
  1073.     "    y Yes, overwrite existing file (Z)",
  1074.     "- as pathname sends standard input as sPID.sz or environment ONAME",
  1075.     ""
  1076. };
  1077.  
  1078. usage()
  1079. {
  1080.     char **pp;
  1081.  
  1082.     for (pp=babble; **pp; ++pp)
  1083.         fprintf(stderr, "%s\n", *pp);
  1084.     fprintf(stderr, "%s for %s by Chuck Forsberg, Omen Technology INC\n",
  1085.      VERSION, OS);
  1086.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  1087.     cucheck();
  1088.     exit(SS_NORMAL);
  1089. }
  1090.  
  1091. /*
  1092.  * Get the receiver's init parameters
  1093.  */
  1094. getzrxinit()
  1095. {
  1096.     register n;
  1097.     struct stat f;
  1098.  
  1099.     for (n=10; --n>=0; ) {
  1100.         
  1101.         switch (zgethdr(Rxhdr, 1)) {
  1102.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  1103.             stohdr(Rxpos);
  1104.             zshhdr(ZACK, Txhdr);
  1105.             continue;
  1106.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  1107.             stohdr(0L);
  1108.             zshhdr(ZRQINIT, Txhdr);
  1109.             continue;
  1110.         case ZRINIT:
  1111.             Rxflags = 0377 & Rxhdr[ZF0];
  1112.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1113.             Zctlesc |= Rxflags & TESCCTL;
  1114.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1115.             if ( !(Rxflags & CANFDX))
  1116.                 Txwindow = 0;
  1117.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1118.             if ( !Fromcu)
  1119.                 signal(SIGINT, SIG_IGN);
  1120. #ifdef MODE2OK
  1121.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1122. #endif
  1123. #ifndef READCHECK
  1124. #ifndef USG
  1125.             /* Use 1024 byte frames if no sample/interrupt */
  1126.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1127.                 Rxbuflen = 1024;
  1128.                 vfile("Rxbuflen=%d", Rxbuflen);
  1129.             }
  1130. #endif
  1131. #endif
  1132.             /* Override to force shorter frame length */
  1133.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1134.                 Rxbuflen = Tframlen;
  1135.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1136.                 Rxbuflen = Tframlen;
  1137.             vfile("Rxbuflen=%d", Rxbuflen);
  1138.  
  1139. #ifndef vax11c
  1140.             /* If using a pipe for testing set lower buf len */
  1141.             fstat(iofd, &f);
  1142.             if ((f.st_mode & S_IFMT) != S_IFCHR) {
  1143.                 Rxbuflen = 1024;
  1144.             }
  1145. #endif
  1146. #ifdef BADSEEK
  1147.             Canseek = 0;
  1148.             Txwindow = TXBSIZE - 1024;
  1149.             Txwspac = TXBSIZE/4;
  1150. #endif
  1151.             /*
  1152.              * If input is not a regular file, force ACK's to
  1153.              *  prevent running beyond the buffer limits
  1154.              */
  1155.             if ( !Command) {
  1156.                 fstat(fileno(in), &f);
  1157.                 if ((f.st_mode & S_IFMT) != S_IFREG) {
  1158.                     Canseek = -1;
  1159. #ifdef TXBSIZE
  1160.                     Txwindow = TXBSIZE - 1024;
  1161.                     Txwspac = TXBSIZE/4;
  1162. #else
  1163.                     return ERROR;
  1164. #endif
  1165.                 }
  1166.             }
  1167.             /* Set initial subpacket length */
  1168.             if (blklen < 1024) {    /* Command line override? */
  1169.                 if (Baudrate > 300)
  1170.                     blklen = 256;
  1171.                 if (Baudrate > 1200)
  1172.                     blklen = 512;
  1173.                 if (Baudrate > 2400)
  1174.                     blklen = 1024;
  1175.             }
  1176.             if (Rxbuflen && blklen>Rxbuflen)
  1177.                 blklen = Rxbuflen;
  1178.             if (blkopt && blklen > blkopt)
  1179.                 blklen = blkopt;
  1180.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1181.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1182.  
  1183.             return (sendzsinit());
  1184.         case ZCAN:
  1185.         case TIMEOUT:
  1186.             return ERROR;
  1187.         case ZRQINIT:
  1188.             if (Rxhdr[ZF0] == ZCOMMAND)
  1189.                 continue;
  1190.         default:
  1191.             zshhdr(ZNAK, Txhdr);
  1192.             continue;
  1193.         }
  1194.     }
  1195.     return ERROR;
  1196. }
  1197.  
  1198. /* Send send-init information */
  1199. sendzsinit()
  1200. {
  1201.     register c;
  1202.  
  1203.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1204.         return OK;
  1205.     errors = 0;
  1206.     for (;;) {
  1207.         stohdr(0L);
  1208.         if (Zctlesc) {
  1209.             Txhdr[ZF0] |= TESCCTL; zshhdr(ZSINIT, Txhdr);
  1210.         }
  1211.         else
  1212.             zsbhdr(ZSINIT, Txhdr);
  1213.         zsdata(Myattn, 1+strlen(Myattn), ZCRCW);
  1214.         c = zgethdr(Rxhdr, 1);
  1215.         switch (c) {
  1216.         case ZCAN:
  1217.             return ERROR;
  1218.         case ZACK:
  1219.             return OK;
  1220.         default:
  1221.             if (++errors > 19)
  1222.                 return ERROR;
  1223.             continue;
  1224.         }
  1225.     }
  1226. }
  1227.  
  1228. /* Send file name and related info */
  1229. zsendfile(buf, blen)
  1230. char *buf;
  1231. {
  1232.     register c;
  1233.     register UNSL long crc;
  1234.  
  1235.     for (;;) {
  1236.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1237.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1238.         if (Lskipnocor)
  1239.             Txhdr[ZF1] |= ZMSKNOLOC;
  1240.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1241.         Txhdr[ZF3] = 0;
  1242.         zsbhdr(ZFILE, Txhdr);
  1243.         zsdata(buf, blen, ZCRCW);
  1244. again:
  1245.         c = zgethdr(Rxhdr, 1);
  1246.         switch (c) {
  1247.         case ZRINIT:
  1248.             while ((c = readline(50)) > 0)
  1249.                 if (c == ZPAD) {
  1250.                     goto again;
  1251.                 }
  1252.             /* **** FALL THRU TO **** */
  1253.         default:
  1254.             continue;
  1255.         case ZCAN:
  1256.         case TIMEOUT:
  1257.         case ZABORT:
  1258.         case ZFIN:
  1259.             return ERROR;
  1260.         case ZCRC:
  1261.             crc = 0xFFFFFFFFL;
  1262.             if (Canseek >= 0) {
  1263.                 while (((c = getc(in)) != EOF) && --Rxpos)
  1264.                     crc = UPDC32(c, crc);
  1265.                 crc = ~crc;
  1266.                 clearerr(in);    /* Clear EOF */
  1267.                 fseek(in, 0L, 0);
  1268.             }
  1269.             stohdr(crc);
  1270.             zsbhdr(ZCRC, Txhdr);
  1271.             goto again;
  1272.         case ZSKIP:
  1273.             fclose(in); return c;
  1274.         case ZRPOS:
  1275.             /*
  1276.              * Suppress zcrcw request otherwise triggered by
  1277.              * lastyunc==bytcnt
  1278.              */
  1279.             if (Rxpos && fseek(in, Rxpos, 0))
  1280.                 return ERROR;
  1281.             Lastsync = (bytcnt = Txpos = Rxpos) -1;
  1282.             return zsendfdata();
  1283.         }
  1284.     }
  1285. }
  1286.  
  1287. /* Send the data in the file */
  1288. zsendfdata()
  1289. {
  1290.     register c, e, n;
  1291.     register newcnt;
  1292.     register long tcount = 0;
  1293.     int junkcount;        /* Counts garbage chars received by TX */
  1294.     static int tleft = 6;    /* Counter for test mode */
  1295.  
  1296.     Lrxpos = 0;
  1297.     junkcount = 0;
  1298.     Beenhereb4 = FALSE;
  1299. somemore:
  1300.     if (setjmp(intrjmp)) {
  1301. waitack:
  1302.         junkcount = 0;
  1303.         c = getinsync(0);
  1304. gotack:
  1305.         switch (c) {
  1306.         default:
  1307.         case ZCAN:
  1308.             fclose(in);
  1309.             return ERROR;
  1310.         case ZSKIP:
  1311.             fclose(in);
  1312.             return c;
  1313.         case ZACK:
  1314.         case ZRPOS:
  1315.             break;
  1316.         case ZRINIT:
  1317.             return OK;
  1318.         }
  1319. #ifdef READCHECK
  1320.         /*
  1321.          * If the reverse channel can be tested for data,
  1322.          *  this logic may be used to detect error packets
  1323.          *  sent by the receiver, in place of setjmp/longjmp
  1324.          *  rdchk(fdes) returns non 0 if a character is available
  1325.          */
  1326.         while (rdchk(iofd)) {
  1327. #ifdef SV
  1328.             switch (checked)
  1329. #else
  1330.             switch (readline(1))
  1331. #endif
  1332.             {
  1333.             case CAN:
  1334.             case ZPAD:
  1335.                 c = getinsync(1);
  1336.                 goto gotack;
  1337.             case XOFF:        /* Wait a while for an XON */
  1338.             case XOFF|0200:
  1339.                 readline(100);
  1340.             }
  1341.         }
  1342. #endif
  1343.     }
  1344.  
  1345.     if ( !Fromcu)
  1346.         signal(SIGINT, onintr);
  1347.     newcnt = Rxbuflen;
  1348.     Txwcnt = 0;
  1349.     stohdr(Txpos);
  1350.     zsbhdr(ZDATA, Txhdr);
  1351.  
  1352.     /*
  1353.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1354.      *  many times.  Each time the signal should be caught, causing the
  1355.      *  file to be started over from the beginning.
  1356.      */
  1357.     if (Test) {
  1358.         if ( --tleft)
  1359.             while (tcount < 20000) {
  1360.                 printf(qbf); fflush(stdout);
  1361.                 tcount += strlen(qbf);
  1362. #ifdef READCHECK
  1363.                 while (rdchk(iofd)) {
  1364. #ifdef SV
  1365.                     switch (checked)
  1366. #else
  1367.                     switch (readline(1))
  1368. #endif
  1369.                     {
  1370.                     case CAN:
  1371.                     case ZPAD:
  1372. #ifdef TCFLSH
  1373.                         ioctl(iofd, TCFLSH, 1);
  1374. #endif
  1375.                         goto waitack;
  1376.                     case XOFF:    /* Wait for XON */
  1377.                     case XOFF|0200:
  1378.                         readline(100);
  1379.                     }
  1380.                 }
  1381. #endif
  1382.             }
  1383.         signal(SIGINT, SIG_IGN); canit();
  1384.         sleep(3); purgeline(); mode(0);
  1385.         printf("\nsz: Tcount = %ld\n", tcount);
  1386.         if (tleft) {
  1387.             printf("ERROR: Interrupts Not Caught\n");
  1388.             exit(1);
  1389.         }
  1390.         exit(SS_NORMAL);
  1391.     }
  1392.  
  1393.     do {
  1394.         n = zfilbuf();
  1395.         if (Eofseen)
  1396.             e = ZCRCE;
  1397.         else if (junkcount > 3)
  1398.             e = ZCRCW;
  1399.         else if (bytcnt == Lastsync)
  1400.             e = ZCRCW;
  1401.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1402.             e = ZCRCW;
  1403.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1404.             Txwcnt = 0;  e = ZCRCQ;
  1405.         }
  1406.         else
  1407.             e = ZCRCG;
  1408.         if (Verbose>1)
  1409.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1410.               Txpos, Crc32t?" CRC-32":"");
  1411.         zsdata(txbuf, n, e);
  1412.         bytcnt = Txpos += n;
  1413.         if (e == ZCRCW)
  1414.             goto waitack;
  1415. #ifdef READCHECK
  1416.         /*
  1417.          * If the reverse channel can be tested for data,
  1418.          *  this logic may be used to detect error packets
  1419.          *  sent by the receiver, in place of setjmp/longjmp
  1420.          *  rdchk(fdes) returns non 0 if a character is available
  1421.          */
  1422.         fflush(stdout);
  1423.         while (rdchk(iofd)) {
  1424. #ifdef SV
  1425.             switch (checked)
  1426. #else
  1427.             switch (readline(1))
  1428. #endif
  1429.             {
  1430.             case CAN:
  1431.             case ZPAD:
  1432.                 c = getinsync(1);
  1433.                 if (c == ZACK)
  1434.                     break;
  1435. #ifdef TCFLSH
  1436.                 ioctl(iofd, TCFLSH, 1);
  1437. #endif
  1438.                 /* zcrce - dinna wanna starta ping-pong game */
  1439.                 zsdata(txbuf, 0, ZCRCE);
  1440.                 goto gotack;
  1441.             case XOFF:        /* Wait a while for an XON */
  1442.             case XOFF|0200:
  1443.                 readline(100);
  1444.             default:
  1445.                 ++junkcount;
  1446.             }
  1447.         }
  1448. #endif    /* READCHECK */
  1449.         if (Txwindow) {
  1450.             while ((tcount = Txpos - Lrxpos) >= Txwindow) {
  1451.                 vfile("%ld window >= %u", tcount, Txwindow);
  1452.                 if (e != ZCRCQ)
  1453.                     zsdata(txbuf, 0, e = ZCRCQ);
  1454.                 c = getinsync(1);
  1455.                 if (c != ZACK) {
  1456. #ifdef TCFLSH
  1457.                     ioctl(iofd, TCFLSH, 1);
  1458. #endif
  1459.                     zsdata(txbuf, 0, ZCRCE);
  1460.                     goto gotack;
  1461.                 }
  1462.             }
  1463.             vfile("window = %ld", tcount);
  1464.         }
  1465.     } while (!Eofseen);
  1466.     if ( !Fromcu)
  1467.         signal(SIGINT, SIG_IGN);
  1468.  
  1469.     for (;;) {
  1470.         stohdr(Txpos);
  1471.         zsbhdr(ZEOF, Txhdr);
  1472.         switch (getinsync(0)) {
  1473.         case ZACK:
  1474.             continue;
  1475.         case ZRPOS:
  1476.             goto somemore;
  1477.         case ZRINIT:
  1478.             return OK;
  1479.         case ZSKIP:
  1480.             fclose(in);
  1481.             return c;
  1482.         default:
  1483.             fclose(in);
  1484.             return ERROR;
  1485.         }
  1486.     }
  1487. }
  1488.  
  1489. /*
  1490.  * Respond to receiver's complaint, get back in sync with receiver
  1491.  */
  1492. getinsync(flag)
  1493. {
  1494.     register c;
  1495.  
  1496.     for (;;) {
  1497.         if (Test) {
  1498.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1499.             Rxpos = 0; c = ZRPOS;
  1500.         } else
  1501.             c = zgethdr(Rxhdr, 0);
  1502.         switch (c) {
  1503.         case ZCAN:
  1504.         case ZABORT:
  1505.         case ZFIN:
  1506.         case TIMEOUT:
  1507.             return ERROR;
  1508.         case ZRPOS:
  1509.             /* ************************************* */
  1510.             /*  If sending to a buffered modem, you  */
  1511.             /*   might send a break at this point to */
  1512.             /*   dump the modem's buffer.         */
  1513.             clearerr(in);    /* In case file EOF seen */
  1514.             if (fseek(in, Rxpos, 0))
  1515.                 return ERROR;
  1516.             Eofseen = 0;
  1517.             bytcnt = Lrxpos = Txpos = Rxpos;
  1518.             if (Lastsync == Rxpos) {
  1519.                 if (++Beenhereb4 > 4)
  1520.                     if (blklen > 32)
  1521.                         blklen /= 2;
  1522.             }
  1523.             Lastsync = Rxpos;
  1524.             return c;
  1525.         case ZACK:
  1526.             Lrxpos = Rxpos;
  1527.             if (flag || Txpos == Rxpos)
  1528.                 return ZACK;
  1529.             continue;
  1530.         case ZRINIT:
  1531.         case ZSKIP:
  1532.             fclose(in);
  1533.             return c;
  1534.         case ERROR:
  1535.         default:
  1536.             zsbhdr(ZNAK, Txhdr);
  1537.             continue;
  1538.         }
  1539.     }
  1540. }
  1541.  
  1542.  
  1543. /* Say "bibi" to the receiver, try to do it cleanly */
  1544. saybibi()
  1545. {
  1546.     for (;;) {
  1547.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  1548.         zshhdr(ZFIN, Txhdr);    /*  to make debugging easier */
  1549.         switch (zgethdr(Rxhdr, 0)) {
  1550.         case ZFIN:
  1551.             sendline('O'); sendline('O'); flushmo();
  1552.         case ZCAN:
  1553.         case TIMEOUT:
  1554.             return;
  1555.         }
  1556.     }
  1557. }
  1558.  
  1559. /* Local screen character display function */
  1560. bttyout(c)
  1561. {
  1562.     if (Verbose)
  1563.         putc(c, stderr);
  1564. }
  1565.  
  1566. /* Send command and related info */
  1567. zsendcmd(buf, blen)
  1568. char *buf;
  1569. {
  1570.     register c;
  1571.     long cmdnum;
  1572.  
  1573.     cmdnum = getpid();
  1574.     errors = 0;
  1575.     for (;;) {
  1576.         stohdr(cmdnum);
  1577.         Txhdr[ZF0] = Cmdack1;
  1578.         zsbhdr(ZCOMMAND, Txhdr);
  1579.         zsdata(buf, blen, ZCRCW);
  1580. listen:
  1581.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1582.         c = zgethdr(Rxhdr, 1);
  1583.  
  1584.         switch (c) {
  1585.         case ZRINIT:
  1586.             goto listen;    /* CAF 8-21-87 */
  1587.         case ERROR:
  1588.         case TIMEOUT:
  1589.             if (++errors > Cmdtries)
  1590.                 return ERROR;
  1591.             continue;
  1592.         case ZCAN:
  1593.         case ZABORT:
  1594.         case ZFIN:
  1595.         case ZSKIP:
  1596.         case ZRPOS:
  1597.             return ERROR;
  1598.         default:
  1599.             if (++errors > 20)
  1600.                 return ERROR;
  1601.             continue;
  1602.         case ZCOMPL:
  1603.             Exitcode = Rxpos;
  1604.             saybibi();
  1605.             return OK;
  1606.         case ZRQINIT:
  1607. #ifdef vax11c        /* YAMP :== Yet Another Missing Primitive */
  1608.             return ERROR;
  1609. #else
  1610.             vfile("******** RZ *******");
  1611.             system("rz");
  1612.             vfile("******** SZ *******");
  1613.             goto listen;
  1614. #endif
  1615.         }
  1616.     }
  1617. }
  1618.  
  1619. /*
  1620.  * If called as sb use YMODEM protocol
  1621.  */
  1622. chkinvok(s)
  1623. char *s;
  1624. {
  1625. #ifdef vax11c
  1626.     Progname = "sz";
  1627. #else
  1628.     register char *p;
  1629.  
  1630.     p = s;
  1631.     while (*p == '-')
  1632.         s = ++p;
  1633.     while (*p)
  1634.         if (*p++ == '/')
  1635.             s = p;
  1636.     if (*s == 'v') {
  1637.         Verbose=1; ++s;
  1638.     }
  1639.     Progname = s;
  1640.     if (s[0]=='s' && s[1]=='b') {
  1641.         Nozmodem = TRUE; blklen=1024;
  1642.     }
  1643.     if (s[0]=='s' && s[1]=='x') {
  1644.         Modem2 = TRUE;
  1645.     }
  1646. #endif
  1647. }
  1648.  
  1649. countem(argc, argv)
  1650. register char **argv;
  1651. {
  1652.     register c;
  1653.     struct stat f;
  1654.  
  1655.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1656.         f.st_size = -1;
  1657.         if (Verbose>2) {
  1658.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1659.             fflush(stderr);
  1660.         }
  1661.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1662.             c = f.st_mode & S_IFMT;
  1663.             if (c != S_IFDIR && c != S_IFBLK) {
  1664.                 ++Filesleft;  Totalleft += f.st_size;
  1665.             }
  1666.         }
  1667.         if (Verbose>2)
  1668.             fprintf(stderr, " %ld", f.st_size);
  1669.     }
  1670.     if (Verbose>2)
  1671.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1672.           Filesleft, Totalleft);
  1673. }
  1674.  
  1675. chartest(m)
  1676. {
  1677.     register n;
  1678.  
  1679.     mode(m);
  1680.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1681.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1682.     printf("Hit Enter.\021");  fflush(stdout);
  1683.     readline(500);
  1684.  
  1685.     for (n = 0; n < 256; ++n) {
  1686.         if (!(n%8))
  1687.             printf("\r\n");
  1688.         printf("%02x ", n);  fflush(stdout);
  1689.         sendline(n);    flushmo();
  1690.         printf("  ");  fflush(stdout);
  1691.         if (n == 127) {
  1692.             printf("Hit Enter.\021");  fflush(stdout);
  1693.             readline(500);
  1694.             printf("\r\n");  fflush(stdout);
  1695.         }
  1696.     }
  1697.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1698.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1699.  
  1700.     while (n != TIMEOUT && n != ' ') {
  1701.         n = readline(400);
  1702.         printf("%02x\r\n", n);
  1703.         fflush(stdout);
  1704.     }
  1705.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1706.     fflush(stdout);
  1707. }
  1708.  
  1709. /* End of sz.c */
  1710.